Public Partial Class ShowThread
    Inherits System.Web.UI.Page
    Dim threadPostID As String = String.Empty
    Dim profiles As New Hashtable()

    Private Sub ShowThread_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        gvwPosts.PageSize = 10
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        threadPostID = Me.Request.QueryString("ID")

        If Not Me.IsPostBack Then
            threadPostID = Me.Request.QueryString("ID")
            Dim post As KDESolutions.Providers.Forums.DataTypes.PostDetails = KDESolutions.Providers.Forums.ForumService.GetPostByID(threadPostID)
            KDESolutions.Providers.Forums.ForumService.IncrementPostViewCount(post.ID)
            Me.Title = String.Format(Me.Title, post.Title)
            lblPageTitle.Text = String.Format(lblPageTitle.Text, post.ForumID, post.ForumTitle, post.Title)
            ShowCommandButtons(post.Closed, post.ForumID, threadPostID, post.AddedBy)
            gvwPosts.DataSource = KDESolutions.Providers.Forums.ForumService.GetThreadByID(threadPostID)
            gvwPosts.DataBind()
        End If
    End Sub


    ' Returns the poster level description, according to the input post count
    Protected Function GetPosterDescription(ByVal posts As Integer) As String
        If posts >= 3000 Then
            Return "Pro Poster"
        ElseIf posts >= 1000 Then
            Return "Superstart"
        ElseIf posts >= 500 Then
            Return "Frequent Poster"
        Else
            Return "Newby"
        End If
    End Function

    Private Sub gvwPosts_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvwPosts.RowCommand
        If e.CommandName = "DeleteThread" Then
            Dim threadPostID As String = e.CommandArgument
            Dim forumID As String = KDESolutions.Providers.Forums.ForumService.GetPostByID(threadPostID).ID
            KDESolutions.Providers.Forums.ForumService.DeletePost(threadPostID)
            Me.Response.Redirect("BrowseThreads.aspx?ForumID=" & forumID.ToString())
        ElseIf e.CommandName = "DeletePost" Then
            Dim postID As String = e.CommandArgument
            KDESolutions.Providers.Forums.ForumService.DeletePost(postID)
            gvwPosts.PageIndex = 0
            gvwPosts.DataBind()
        End If
    End Sub
    Private Sub ShowCommandButtons(ByVal isClosed As Boolean, ByVal forumID As String, ByVal threadPostID As String, ByVal addedBy As String)
        If isClosed Then
            lnkNewReply1.Visible = False
            lnkNewReply2.Visible = False
            btnCloseThread1.Visible = False
            btnCloseThread2.Visible = False
            panClosed.Visible = True
        Else
            lnkNewReply1.NavigateUrl = String.Format(lnkNewReply1.NavigateUrl, forumID, threadPostID)
            lnkNewReply2.NavigateUrl = lnkNewReply1.NavigateUrl
            btnCloseThread1.Visible = (Me.User.Identity.IsAuthenticated And _
               (Me.User.Identity.Name.ToLower().Equals(addedBy) Or _
               (Me.User.IsInRole("Administrators") Or Me.User.IsInRole("Editors") Or Me.User.IsInRole("Moderators"))))
            btnCloseThread2.Visible = btnCloseThread1.Visible
        End If
    End Sub

    Private Sub btnCloseThread1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCloseThread1.Click
        KDESolutions.Providers.Forums.ForumService.CloseThread(threadPostID)
        ShowCommandButtons(True, 0, 0, "")
        gvwPosts.DataBind()
    End Sub

    Private Sub gvwPosts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvwPosts.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim post As KDESolutions.Providers.Forums.DataTypes.PostDetails = CType(e.Row.DataItem, KDESolutions.Providers.Forums.DataTypes.PostDetails)
            Dim threadID As String = post.ParentPostID
            If post.IsFirstPost Then threadID = post.ID

            ' the link for editing the post is visible to the post's author, and to
            ' administrators, editors and moderators
            Dim lnkEditPost As HyperLink = CType(e.Row.FindControl("lnkEditPost"), HyperLink)
            lnkEditPost.NavigateUrl = String.Format(lnkEditPost.NavigateUrl, post.ForumID, threadID, post.ID)
            lnkEditPost.Visible = (Me.User.Identity.IsAuthenticated And _
               (Me.User.Identity.Name.ToLower().Equals(post.AddedBy.ToLower()) Or _
               (Me.User.IsInRole("Administrators") Or Me.User.IsInRole("Editors") Or Me.User.IsInRole("Moderators"))))

            ' the link for deleting the thread/post is visible only to administrators, editors and moderators
            Dim btnDeletePost As ImageButton = CType(e.Row.FindControl("btnDeletePost"), ImageButton)
            If post.IsFirstPost Then
                btnDeletePost.OnClientClick = String.Format(btnDeletePost.OnClientClick, "entire thread")
                btnDeletePost.CommandName = "DeleteThread"
            Else
                btnDeletePost.OnClientClick = String.Format(btnDeletePost.OnClientClick, "post")
                btnDeletePost.CommandName = "DeletePost"
            End If
            btnDeletePost.CommandArgument = post.ID.ToString()
            btnDeletePost.Visible = (Me.User.IsInRole("Administrators") Or Me.User.IsInRole("Editors") Or Me.User.IsInRole("Moderators"))

            ' if the thread is not closed, show the link to quote the post
            Dim lnkQuotePost As HyperLink = CType(e.Row.FindControl("lnkQuotePost"), HyperLink)
            lnkQuotePost.NavigateUrl = String.Format(lnkQuotePost.NavigateUrl, _
               post.ForumID, threadID, post.ID)
            If post.IsFirstPost Then
                lnkQuotePost.Visible = Not post.Closed
            Else
                lnkQuotePost.Visible = Not post.ParentPost.Closed
            End If
        End If
    End Sub

    Protected Sub btnCloseThread_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseThread2.Click
        KDESolutions.Providers.Forums.ForumService.CloseThread(threadPostID)
        ShowCommandButtons(True, 0, 0, "")
        gvwPosts.DataBind()
    End Sub

    Private Sub gvwPosts_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvwPosts.RowDeleting

    End Sub
End Class